package cn.zhku.user.web.servlet.admin;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.itcast.commons.CommonUtils;
import cn.itcast.servlet.BaseServlet;
import cn.zhku.user.domain.Admin;
import cn.zhku.user.domain.User;
import cn.zhku.user.service.UserException;
import cn.zhku.user.service.UserService;

/**
 * Servlet implementation class AdminServlet
 */
@WebServlet("/AdminServlet")
public class AdminServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	UserService userService = new UserService();
	UserServlet userServlet = new UserServlet();
    
	public void adminLogin(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");//
		response.setContentType("text/html;charset=utf-8");
		Admin admin = CommonUtils.toBean(request.getParameterMap(), Admin.class);
		
		try {
			userService.AdminLogin(admin);
			userServlet.doGet(request, response);
		} catch (UserException e) {
			request.setAttribute("msg", e.getMessage());
			request.getRequestDispatcher("user-view/admin/login.jsp").forward(request, response);
		}
	}
	
	public void delete(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");//
		response.setContentType("text/html;charset=utf-8"); 
		String userId = request.getParameter("userId");
		try {
			userService.delete(Integer.parseInt(userId));
		} catch (UserException e) {
			
		}
		
		doGet( request, response);	
	}
	
}
